// pages/mine/address/address_edit.js
Page({

  
  data: {
    region: [],
    customItem: '',name:'',phone:'',address:'',id:"",switch:false,phoneflag:true
  },

  
  onLoad: function (options) {
    //路由传过来的id
    const id=options.id
    if(options.type==1){
      this.setData({switch:true})
    }else{this.setData({switch:false})}
    this.setData({
      id,
      
    })
    const uid=wx.getStorageSync('uid')
    const status=1
    const params={id,status}
    wx.request({
      url: 'https://zujieba.com/api/address/edit_address.html',
      data:params,
      success:(res)=>{
        console.log(res);
        
        const name=res.data.data.name
        const phone=res.data.data.phone
        
        const select=res.data.data.province+","+res.data.data.city+","+res.data.data.area
        const s=select.split(',')
        const address=res.data.data.address
        this.setData({
          name,
          phone,
          region:s,
          address


        })
     
      },
      fail(e){
        console.log(e);
        
      }
    })
    

  },
  //点击确认修改地址按钮
  sureclick(){
    if(this.data.phoneflag==false){return wx.showToast({
      title: '手机输入不正确',
      icon:'none'
    })}
    if(this.data.name==''||this.data.phone==''||this.data.address==''||this.data.region==''){
      return wx.showToast({
        title: '请填写完整',
      })
  }




    const uid=wx.getStorageSync('uid')
    const id=this.data.id
    const name=this.data.name
    const province=this.data.region[0]
    const city=this.data.region[1]
    const area=this.data.region[2]
    const address=this.data.address
    const type=this.data.switch==true?1:2
    const status=2
    const phone=this.data.phone
    const params={uid,id,name,phone,area,province,city,address,status,type}
    console.log(params);
    
    wx.request({
      url: 'https://zujieba.com/api/address/edit_address.html',
      data:params,
      success:(res)=>{
         wx.showToast({
           title: '修改成功',
           icon:'success',
           duration:2000
         })
        wx.navigateBack({
          complete: (res) => {},
        })
        
      }
    })
      

  },
  //获取用户更改后的phone
  phoneclick(e){
   
     const phone=e.detail.value
     
   let phoneflag=  this.checkMobile(phone)
   this.setData({
    phone,
    phoneflag
  })
     

  },
  phoneclick1(e){
     this.checkMobile1(e.detail.value)
  },
  //获取用户更改后的name
  nameclick(e){
    if(e.detail.value==''){
      wx.showToast({
        title: '不能为空',
      })
    }
    const name=e.detail.value
    this.setData({
      name
    })
    

  },
  //获取用户更改后的详细地址
  addressclick(e){
    if(e.detail.value==''){
      wx.showToast({
        title: '不能为空',
      })
    }
    const address=e.detail.value
    this.setData({
      address
    })

  },

  
 
  // 地址选择器
  bindRegionChange: function (e) {
    if(e.detail.value==''){
      wx.showToast({
        title: '不能为空',
      })
    }
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  //点击取消
  cancelclick(){
    wx.navigateTo({
      url: '/pages/mine/address/address',
    })
  },
  //switch开关
  switchchange(e){
    console.log(e.detail.value);
    this.setData({
      switch:e.detail.value
    })
    
  },
 //点击删除按钮
 removeclick(){
 
  
 wx.showModal({
   content:"确认删除该地址吗",
   
   showCancel:true,
   complete:(res)=>{
      if(res.confirm==true){
        const uid=wx.getStorageSync('uid')
        const id=this.data.id
        const params={uid,id}
        console.log(params);
        
        wx.request({
          url: 'https://zujieba.com/api/address/del_address.html',
          data:params,
          success:(res)=>{
            console.log(res);
            if(res.success==1){
              wx.showToast({
                title: res.data.tips,
                icon:"success",
                duration:2000
                
              })
            }else{
              wx.showToast({
                title: res.data.tips,
                icon:'success',
                duration:2000

              })
            }
           
           wx.navigateBack({
             complete: (res) => {},
           })
            
          },
          fail(){
            wx.showToast({
              title: '删除失败',
              icon:'loading',
              duration:2000
            })
          }
        })
      }
      
   }
 })
},
checkMobile(e){
    
  if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(e))){
     
      
      return false;
  }
  else{
    return true
  }
},
checkMobile1(e){

if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(e))){
    wx.showToast({
      title: '请输入正确的手机号',
      icon:'none'
    })
    
    return false;
}
else{
  return true
}
}


})